import PageFilterComponent from '@/components/PageFilter/index.vue';
import { CustomerApi, WorkflowApi } from '@/api';
import { AdminUserMapping, MappingTools, WorkflowMapping } from '@/mappings';
import WorkflowApprovalDialog from '@/views/WorkflowList/components/WorkflowApprovalDialog.vue';
import { mapGetters } from 'vuex';

export default {
  name: 'WorkflowList',
  components: { WorkflowApprovalDialog, PageFilterComponent },
  data() {
    return {
      tableData: {
        page: 1,
        pageSize: 20,
        total: 0,
        list: [],
      },
      filterForm: {
        name: null,
      },
      selection: {},
      approvalDialog: false,
      approvalData: {},
    };
  },
  created() {
    this.fetchTableData();
  },
  computed: {
    ...mapGetters(['user']),
  },
  methods: {
    async fetchTableData() {
      try {
        const response = await WorkflowApi.page({
          page: this.tableData.page,
          pageSize: this.tableData.pageSize,
          ...this.filterForm,
        });
        this.tableData = response.result;
      } catch (e) {
        this.$message.error('数据查询失败：' + JSON.stringify(e));
      }
    },
    onPageChanged(page) {
      this.tableData.page = page;
      this.fetchTableData();
    },
    onPageSizeChanged(pageSize) {
      this.tableData.pageSize = pageSize;
      this.fetchTableData();
    },
    matchTableFieldLabelTodoType(row, column, cellValue) {
      return MappingTools.matchLabel(WorkflowMapping.TodoType, cellValue);
    },
    matchTableFieldLabelStatus(row, column, cellValue) {
      return MappingTools.matchLabel(WorkflowMapping.Status, cellValue);
    },
    async showApprovalData(row) {
      this.selection = row;
      const loading = this.$loading({
        lock: true,
        text: '数据请求中',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)',
      });
      try {
        const resp = await WorkflowApi.getApprovalData(row.client_id);
        this.approvalData = resp.result;
        this.approvalDialog = true;
      } catch (e) {
        console.log(e);
        this.$message.error('获取审核数据失败：' + JSON.stringify(e));
      } finally {
        loading.close();
      }
    },
    onApprovalSuccess() {
      this.selection = {};
      this.approvalDialog = false;
      this.fetchTableData();
    },
    onConfirmRejectData(row) {
      this.$confirm('我已明确该待办数据', '提示', {
      }).then(async () => {
        try {
          await WorkflowApi.confirm(row.list_id);
          this.fetchTableData()
        } catch (e) {
          this.$message.error('接口异常：' + JSON.stringify(e));
        }
      }).catch(() => { })
    },
    isAuditor() {
      return MappingTools.valueInMapping(
        this.user.u_level,
        AdminUserMapping.Level.FirstApproval,
        AdminUserMapping.Level.SecondApproval
      )
    }
  },
};
